﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Markup;
using System.Xml.Linq;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Data;
using System.IO.IsolatedStorage;
using System.IO;
using System.Collections.ObjectModel;
namespace ProtoType.DataStorage
{
    public class DataTemplates
    {
        public DataTemplate GetDataTemplate(string layoutype)
        {
            string xaml = string.Empty;
            Uri uri = new Uri("SampleData/Layout.xml", UriKind.Relative);
            XDocument Xdoc = XDocument.Load(uri.ToString());

            var template = from temp in Xdoc.Descendants("layout") where temp.Attribute("Name").Value == layoutype select temp;
            foreach (var t in template)
            {
                xaml = t.Element("template").Element("DataTemplate").ToString();
            }

            xaml = xaml.Replace("<DataTemplate>", "<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>");
            DataTemplate datatemp = XamlReader.Load(xaml) as DataTemplate;
            return datatemp;
        }

        public DataTemplate GetModifiedDataTemplate(string formid, string layoutype)
        {
            string xaml = string.Empty;
            Uri uri = new Uri("SampleData/formdesign.xml", UriKind.Relative);
            XDocument Xdoc = XDocument.Load(uri.ToString());

            var template = from temp in Xdoc.Descendants("form") where temp.Attribute("id").Value == formid select temp;
            foreach (var t in template)
            {
                xaml = t.Element("Template").Value.ToString();
            }

            xaml = xaml.Replace("<DataTemplate>", "<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>");
            DataTemplate datatemp = XamlReader.Load(xaml) as DataTemplate;
            return datatemp;
        }

        public DataTemplate GetModifiedDataTemplate11(string formid, string layoutype)
        {
            string xaml = string.Empty;
            DataTemplate datatemp = new DataTemplate();
            using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {

                IsolatedStorageFileStream isoStream;
                XDocument formlayoutdoc;
                if (isoStore.FileExists("formdesign.xml"))
                {
                    isoStream = new IsolatedStorageFileStream("formdesign.xml", FileMode.Open, FileAccess.Read, isoStore);
                    formlayoutdoc = XDocument.Load(isoStream);
                    isoStream.Close();
                    var template = from temp in formlayoutdoc.Descendants("form") where temp.Attribute("id").Value == formid select temp;
                    foreach (var t in template)
                    {
                        xaml = t.Element("Template").Value.ToString();
                    }

                    xaml = xaml.Replace("<DataTemplate>", "<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>");
                    datatemp = XamlReader.Load(xaml) as DataTemplate;

                }
            }
            return datatemp;

            //Uri uri = new Uri("SampleData/formdesign.xml", UriKind.Relative);
            //XDocument Xdoc = XDocument.Load(uri.ToString());

            //var template = from temp in Xdoc.Descendants("form") where temp.Attribute("id").Value == formid select temp;
            //foreach (var t in template)
            //{
            //    xaml = t.Element("Template").Value.ToString();
            //}

            //xaml = xaml.Replace("<DataTemplate>", "<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>");
            //DataTemplate datatemp = XamlReader.Load(xaml) as DataTemplate;
            //return datatemp;
        }
        public TextBox CreateATextBox(string Name, InputScopeName InsName)
        {

            TextBox txtb = new TextBox();
            txtb.Name = Name;
            txtb.Height = 70;
            txtb.Width = 430;
            txtb.Text = "";
            txtb.InputScope = new InputScope();
            txtb.InputScope.Names.Add(InsName);
            txtb.HorizontalAlignment = HorizontalAlignment.Left;
            txtb.Background = new SolidColorBrush(Colors.White);
            txtb.Foreground = new SolidColorBrush(Colors.Black);

            return txtb;
        }
        public TextBox CreateATextBox(string Name)
        {

            TextBox txtb = new TextBox();
            txtb.Name = Name;
            txtb.Height = 70;
            txtb.Text = "";
            txtb.Background = new SolidColorBrush(Colors.White);
            txtb.Foreground = new SolidColorBrush(Colors.Black);

            return txtb;
        }
        public TextBox CreateATextBox()
        {

            TextBox txtb = new TextBox();

            txtb.Height = 70;
            txtb.Text = "";
            txtb.Background = new SolidColorBrush(Colors.White);
            txtb.Foreground = new SolidColorBrush(Colors.Black);

            return txtb;
        }
        public TextBlock CreateATextBlock(string Name)
        {

            TextBlock tblock = new TextBlock();

            tblock.Height = 40;
            tblock.Text = Name;
            tblock.FontSize = 28;
            tblock.Foreground = new SolidColorBrush(Colors.White);
            tblock.HorizontalAlignment = HorizontalAlignment.Left;

            return tblock;
        }
        public ListBox CreateAListBox(string Name)
        {
            DataTemplates dt = new DataTemplates();
            ListBox lstbox = new ListBox();
            lstbox.SelectionChanged += new SelectionChangedEventHandler(lstbox_SelectionChanged);
            //lstbox.ItemTemplate = dt.GetDataTemplate("Linear", "Name", "Description");
            lstbox.Background = new SolidColorBrush(Colors.Black);
            lstbox.SelectionChanged += new SelectionChangedEventHandler(lstbox_SelectionChanged);
            lstbox.BorderThickness = new Thickness(2);
            lstbox.Height = 200;
            Uri uri = new Uri("SampleData/forms.xml", UriKind.Relative);
            XDocument Xdoc = XDocument.Load(uri.ToString());
            lstbox.ItemsSource = (from book in Xdoc.Root.Elements("form")
                                  select new Form
                                  {
                                      Id = int.Parse(book.Element("id").Value),
                                      Name = book.Element("name").Value,
                                      Description = book.Element("description").Value
                                  }).ToList();

            return lstbox;

        }
        void lstbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        public IEnumerable<DependencyObject> GetChildsRecursive(DependencyObject root)
        {
            List<DependencyObject> elts = new List<DependencyObject>();
            elts.Add(root);
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(root); i++)
            {
                // DependencyObject dobj = VisualTreeHelper.GetChild(root, i);               
                elts.AddRange(GetChildsRecursive(VisualTreeHelper.GetChild(root, i)));
            }

            return elts;
        }


        public class XAttributeConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                var xml = value as XElement;
                var name = parameter as string;
                return xml.Attribute(name).Value;
            }


            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                return null;
            }
        }
       
    }
    public static class StaticClass
    {

        public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> source)
        {
            ObservableCollection<T> obsColl = new ObservableCollection<T>();
            foreach (T element in source)
            {
                obsColl.Add(element);
            }
            return obsColl;
        }

        public static DependencyObject searched = null;

        public static DependencyObject FindChild(DependencyObject dependencyObject, Func<DependencyObject, bool> predicate)
        {
            if (searched != null)
                return searched;
            DependencyObject child = null;
            FrameworkElement frameworkElement = dependencyObject as FrameworkElement;
            if (frameworkElement != null)
            {
                int intCount =
            System.Windows.Media.VisualTreeHelper.GetChildrenCount(frameworkElement);
                for (int i = 0; i < intCount; i++)
                {
                    child = System.Windows.Media.VisualTreeHelper.GetChildrenCount
                (frameworkElement) == 0 ? null :
                System.Windows.Media.VisualTreeHelper.GetChild(frameworkElement, i);
                    if (predicate(child))
                    {
                        searched = child; break;
                    }
                    FindChild(child, predicate);
                }
            }
            return searched;
        }

        public static List<TextBox> GetTextBoxes(UIElement uiElement, List<TextBox> textBoxList)
        {
            TextBox textBox = uiElement as TextBox;
            if (textBox != null)
            {
                // If the UIElement is a Textbox, add it to the list. 
                textBoxList.Add(textBox);
            }
            else
            {
                Panel panel = uiElement as Panel;
                if (panel != null)
                {
                    // If the UIElement is a panel, then loop through it's children 
                    foreach (UIElement child in panel.Children)
                    {
                        GetTextBoxes(child, textBoxList);
                    }
                }
            }
            return textBoxList;
        }

        public static List<RadioButton> GetRadiobuttons(UIElement uiElement, List<RadioButton> radiobuttonlist)
        {
            RadioButton radiobutton = uiElement as RadioButton;
            if (radiobutton != null)
            {
                // If the UIElement is a Textbox, add it to the list. 
                radiobuttonlist.Add(radiobutton);
            }
            else
            {
                Panel panel = uiElement as Panel;
                if (panel != null)
                {
                    // If the UIElement is a panel, then loop through it's children 
                    foreach (UIElement child in panel.Children)
                    {
                        GetRadiobuttons(child, radiobuttonlist);
                    }
                }
            }
            return radiobuttonlist;
        }
    }
}
